package cn.ubeta.b2c.utils;

import android.content.Context;
import android.text.TextUtils;

import cn.ubeta.b2c.BuildConfig;
import cn.ubeta.b2c.DemoData;
import cn.ubeta.b2c.bean.AddressInfo;
import cn.ubeta.b2c.bean.MeInfo;
import cn.ubeta.b2c.bean.OrderInfo;
import cn.ubeta.b2c.bean.UserInfo;
import cn.ubeta.b2c.utils.preference.CommonSetting;
import cn.ubeta.b2c.utils.preference.Installation;
import cn.ubeta.b2c.utils.preference.UserSetting;

import java.util.List;

/**
 * Created by liudengyong on 16/8/22.
 */

public class AppConfig {

    private static Context mContext;
    private static UserSetting mUserSetting;
    private static CommonSetting mCommonSetting;
    private static boolean sPending2Order;

    /**
     * 初始化
     * @param context
     */
    public static void init(Context context) {
        mContext = context;
        // TODO 测试数据,待删除
        DemoData.init(context);
        mCommonSetting = new CommonSetting(mContext, "common_setting_" + getBuildType());
        String userId = mCommonSetting.getCurUserId();
        if (TextUtils.isEmpty(userId)) {
            // 匿名用户
            String uuid = Installation.id(mContext);
            mUserSetting = new UserSetting(mContext, "user_" + uuid + "_setting_" + getBuildType());
        } else {
            // 登录用户
            mUserSetting = new UserSetting(mContext, "user_" + userId + "_setting_" + getBuildType());
        }
    }

    private static Context getContext() {
        return mContext;
    }

    public static String getBuildType() {
        return BuildConfig.BUILD_TYPE;
    }

    public static String getNickName() {
        return mUserSetting.getNickName();
    }

    public static void saveUserInfo(UserInfo info) {
        mUserSetting.saveUserInfo(info);
    }

    public static String getUid() {
        return mUserSetting.getUid();
    }

    public static void setUid(String uid) {
        mUserSetting.setUid(uid);
    }

    public static void setRunGuide() {
        mUserSetting.setRunGuide();
    }

    public static boolean needRunGuide() {
        return mUserSetting.needRunGuide();
    }

    public static void savePreConfigAddress(AddressInfo info) {
        mUserSetting.savePreConfigAddress(info);
    }

    public static AddressInfo getPreConfigAddressInfo() {
        return mUserSetting.getPreConfigAddressInfo();
    }

    public static void savePreConfigJdPayCardInfo(String json) {
        mUserSetting.savePreConfigJdPayCardInfo(json);
    }

    public static String getPreConfigJdPayCardTitle() {
        return mUserSetting.getPreConfigJdPayCardTitle();
    }

    public static void savePreConfigRecvTime(int recvTime) {
        mUserSetting.savePreConfigRecvTime(recvTime);
    }

    public static int getPreConfigRecvTime() {
        return mUserSetting.getPreConfigRecvTime();
    }

    public static void updateLocation(double latitude, double longitude) {
        //sLatitude = latitude;
        //sLongitude = longitude;
    }

    /**
     * 位置参数
     * @return
     */
    public static String getArea() {
        AddressInfo info = getPreConfigAddressInfo();
        if (info.getAddressId() != 0) {
            return String.format("%d,%d,%d,%d", info.getProvinceId(), info.getCityId(), info.getCountyId(), info.getTownId());
        }
        return "1,2800,2848,0";
    }

    public static void savePreConfigBillInfo(String title, boolean isElec, String mobile) {
        mUserSetting.savePreConfigBillInfo(title, isElec, mobile);
    }

    public static String getPreConfigBillInfo() {
        return mUserSetting.getPreConfigBillInfo();
    }

    public static String getPreConfigBillTitle() {
        return mUserSetting.getPreConfigBillTitle();
    }

    public static String getPreConfigBillMobile() {
        return mUserSetting.getPreConfigBillMobile();
    }

    public static boolean getPreConfigBillIsElec() {
        return mUserSetting.getPreConfigBillIsElec();
    }

    public static List<OrderInfo> getOrders() {
        return mUserSetting.getOrders();
    }

    public static void saveOrderFromVr(OrderInfo info) {
        mUserSetting.saveOrder(info);
    }

    public static void cleanPending2Order() {
        sPending2Order = false;
    }

    public static boolean isPending2Order() {
        return sPending2Order;
    }

    public static void saveOrder(OrderInfo info) {
        mUserSetting.saveOrder(info);
    }

    public static void setCurUserId(String pin) {
        mCommonSetting.setCurUserId(pin);
        AppConfig.init(mContext);
    }

    public static void saveMeInfo(MeInfo info) {
        mUserSetting.saveMeInfo(info);
    }

    public static String getAvatar() {
        return mUserSetting.getAvatar();
    }

    private static long sOrderId = 0;

    public static void setPayingOrderId(long orderId) {
        sOrderId = orderId;
    }

    public static void setOrderPaid() {
        if (sOrderId != 0) {
            mUserSetting.setOrderPaid(sOrderId);
            sOrderId = 0;
        }
    }

    public static void gotoPayFromVr() {
        sPending2Order = true;
    }
}
